package com.yaopaikeji.mapper;

import com.github.pagehelper.PageInfo;
import com.yaopaikeji.model.ContactUsInfo;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface ContactUsMapper {

    @Insert("insert into contact_us_info( " +
            "                                name, " +
            "                                email, " +
            "                                subject, " +
            "                                message, " +
            "                                create_time, " +
            "                                status," +
            "                                last_modified_time, " +
            "                                last_modifieder "+
            "                            ) values( " +
            "                                #{name}, " +
            "                                #{email}, " +
            "                                #{subject}, " +
            "                                #{message}, " +
            "                                #{createTime}, " +
            "                                #{status}, " +
            "                                #{lastModifiedTime}, "+
            "                                default" +
            "                            );")
    @SelectKey(statement = "select last_insert_id()",keyProperty = "id",before = false,resultType = int.class)
    int saveContactUsInfo(ContactUsInfo contactUsInfo);

    @Update("<script> " +
            "     update contact_us_info  " +
            "     <set> " +
            "          <if test='status != null'> " +
            "               status = #{status}, " +
            "          </if> " +
            "          <if test='lastModifiedTime != null'> " +
            "               last_modified_time = #{lastModifiedTime}, " +
            "          </if> " +
            "          <if test='lastModifieder != null'> " +
            "               last_modifieder = #{lastModifieder.id} " +
            "          </if> " +
            "     </set>  " +
            "      where id = #{id} " +
            "</script>")
    int updateContactUsInfo(ContactUsInfo contactUsInfo);

    @Select("select * from contact_us_info where id = #{id};")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "subject",property = "subject"),
            @Result(column = "message",property = "message"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "status",property = "status"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_modifieder",property = "lastModifieder",
            one = @One(select = "com.yaopaikeji.mapper.UserMapper.findByPrimaryKey"))
    })
    ContactUsInfo findByPrimaryKey(int id);

    @Select("select * from contact_us_info order by create_time desc;")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "subject",property = "subject"),
            @Result(column = "message",property = "message"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "status",property = "status"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_modifieder",property = "lastModifieder",
                    one = @One(select = "com.yaopaikeji.mapper.UserMapper.findByPrimaryKey"))
    })
    List<ContactUsInfo> findAll();

    @Select("select * from contact_us_info where create_time >= #{startTime} and create_time <= #{endTime} order by create_time desc;")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "subject",property = "subject"),
            @Result(column = "message",property = "message"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "status",property = "status"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_modifieder",property = "lastModifieder",
                    one = @One(select = "com.yaopaikeji.mapper.UserMapper.findByPrimaryKey"))
    })
    List<ContactUsInfo> findByDateInterval(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("select * from contact_us_info order by create_time desc")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "email",property = "email"),
            @Result(column = "subject",property = "subject"),
            @Result(column = "message",property = "message"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "status",property = "status"),
            @Result(column = "last_modified_time",property = "lastModifiedTime"),
            @Result(column = "last_modifieder",property = "lastModifieder",
                    one = @One(select = "com.yaopaikeji.mapper.UserMapper.findByPrimaryKey"))
    })
    List<ContactUsInfo> listByPage();
}
